public class MyLinkendList {
    static class ListNode{
        public int val;
        public ListNode prev;//前驱
        public ListNode next;//后驱

        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;

    //打印
    public void display(){
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;

        }
        System.out.println();
    }

    //长度
    public int size(){
        ListNode cur = head;
        int len = 0;
        while (cur != null){
            len++;
            cur = cur.next;
        }
        return len;
    }

    //查看是否存在key值
    public boolean contains (int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            last = node;
        }else {

            node.next = head;
            head.prev = node;
            head = node;
        }
    }
    //尾插
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
        }else {
            last.next =node;
            node.prev =last;
            last = node;
        }
    }

    //任意位置插入
    public void addIndex(int index,int data){
        if (index < 0 || index > size()){
            throw new ListIndxOutOfException();
        }
        if (index == 0){
            addFirst(data);
        }
        if (index == size()){
            addLast(data);
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndex(index);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;

    }
    public ListNode findIndex(int index){
        ListNode cur = head;
        while (index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //删除第一个key出现的位置
    public void remove(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                //删除的是头节点
                if (cur == head){
                    head = head.next;
                    //只有一个节点
                    if(head != null){
                        head.prev = null;
                    }
                }else {
                    //中间 尾把
                    cur.prev.next = cur.next;
                    //不是尾巴节点
                    if (cur.next != null){
                        cur.next.prev =cur.prev;
                    }else {
                        //是尾巴节点
                        last = last.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有的key
    public void  removeAllkey(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                //删除的是头节点
                if (cur == head){
                    head = head.next;
                    //只有一个节点
                    if(head != null){
                        head.prev = null;
                    }
                }else {
                    //中间 尾把
                    cur.prev.next = cur.next;
                    //不是尾巴节点
                    if (cur.next != null){
                        cur.next.prev =cur.prev;
                    }else {
                        //是尾巴节点
                        last = last.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    //清空
    public void clear(){
        ListNode cur = head;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }

}
